xfs_aops.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 = LINVFS_GET_VP(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 *)NULL,
  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 = LINVFS_GET_VP(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 = LINVFS_GET_VP(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_at_offset(
  433. struct buffer_head *bh,
  434. loff_t offset,
  435. int block_bits,
  436. xfs_iomap_t *iomapp)
  437. {
  438. xfs_daddr_t bn;
  439. int sector_shift;
  440. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  441. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  442. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  443. sector_shift = block_bits - BBSHIFT;
  444. bn = (iomapp->iomap_bn >> sector_shift) +
  445. ((offset - iomapp->iomap_offset) >> block_bits);
  446. ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
  447. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  448. lock_buffer(bh);
  449. bh->b_blocknr = bn;
  450. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  451. set_buffer_mapped(bh);
  452. clear_buffer_delay(bh);
  453. clear_buffer_unwritten(bh);
  454. }
  455. /*
  456. * Look for a page at index that is suitable for clustering.
  457. */
  458. STATIC unsigned int
  459. xfs_probe_page(
  460. struct page *page,
  461. unsigned int pg_offset,
  462. int mapped)
  463. {
  464. int ret = 0;
  465. if (PageWriteback(page))
  466. return 0;
  467. if (page->mapping && PageDirty(page)) {
  468. if (page_has_buffers(page)) {
  469. struct buffer_head *bh, *head;
  470. bh = head = page_buffers(page);
  471. do {
  472. if (!buffer_uptodate(bh))
  473. break;
  474. if (mapped != buffer_mapped(bh))
  475. break;
  476. ret += bh->b_size;
  477. if (ret >= pg_offset)
  478. break;
  479. } while ((bh = bh->b_this_page) != head);
  480. } else
  481. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  482. }
  483. return ret;
  484. }
  485. STATIC size_t
  486. xfs_probe_cluster(
  487. struct inode *inode,
  488. struct page *startpage,
  489. struct buffer_head *bh,
  490. struct buffer_head *head,
  491. int mapped)
  492. {
  493. struct pagevec pvec;
  494. pgoff_t tindex, tlast, tloff;
  495. size_t total = 0;
  496. int done = 0, i;
  497. /* First sum forwards in this page */
  498. do {
  499. if (!buffer_uptodate(bh) || (mapped != buffer_mapped(bh)))
  500. return total;
  501. total += bh->b_size;
  502. } while ((bh = bh->b_this_page) != head);
  503. /* if we reached the end of the page, sum forwards in following pages */
  504. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  505. tindex = startpage->index + 1;
  506. /* Prune this back to avoid pathological behavior */
  507. tloff = min(tlast, startpage->index + 64);
  508. pagevec_init(&pvec, 0);
  509. while (!done && tindex <= tloff) {
  510. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  511. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  512. break;
  513. for (i = 0; i < pagevec_count(&pvec); i++) {
  514. struct page *page = pvec.pages[i];
  515. size_t pg_offset, len = 0;
  516. if (tindex == tlast) {
  517. pg_offset =
  518. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  519. if (!pg_offset) {
  520. done = 1;
  521. break;
  522. }
  523. } else
  524. pg_offset = PAGE_CACHE_SIZE;
  525. if (page->index == tindex && !TestSetPageLocked(page)) {
  526. len = xfs_probe_page(page, pg_offset, mapped);
  527. unlock_page(page);
  528. }
  529. if (!len) {
  530. done = 1;
  531. break;
  532. }
  533. total += len;
  534. tindex++;
  535. }
  536. pagevec_release(&pvec);
  537. cond_resched();
  538. }
  539. return total;
  540. }
  541. /*
  542. * Test if a given page is suitable for writing as part of an unwritten
  543. * or delayed allocate extent.
  544. */
  545. STATIC int
  546. xfs_is_delayed_page(
  547. struct page *page,
  548. unsigned int type)
  549. {
  550. if (PageWriteback(page))
  551. return 0;
  552. if (page->mapping && page_has_buffers(page)) {
  553. struct buffer_head *bh, *head;
  554. int acceptable = 0;
  555. bh = head = page_buffers(page);
  556. do {
  557. if (buffer_unwritten(bh))
  558. acceptable = (type == IOMAP_UNWRITTEN);
  559. else if (buffer_delay(bh))
  560. acceptable = (type == IOMAP_DELAY);
  561. else if (buffer_mapped(bh))
  562. acceptable = (type == 0);
  563. else
  564. break;
  565. } while ((bh = bh->b_this_page) != head);
  566. if (acceptable)
  567. return 1;
  568. }
  569. return 0;
  570. }
  571. /*
  572. * Allocate & map buffers for page given the extent map. Write it out.
  573. * except for the original page of a writepage, this is called on
  574. * delalloc/unwritten pages only, for the original page it is possible
  575. * that the page has no mapping at all.
  576. */
  577. STATIC int
  578. xfs_convert_page(
  579. struct inode *inode,
  580. struct page *page,
  581. loff_t tindex,
  582. xfs_iomap_t *mp,
  583. xfs_ioend_t **ioendp,
  584. struct writeback_control *wbc,
  585. int startio,
  586. int all_bh)
  587. {
  588. struct buffer_head *bh, *head;
  589. xfs_off_t end_offset;
  590. unsigned long p_offset;
  591. unsigned int type;
  592. int bbits = inode->i_blkbits;
  593. int len, page_dirty;
  594. int count = 0, done = 0, uptodate = 1;
  595. xfs_off_t offset = page_offset(page);
  596. if (page->index != tindex)
  597. goto fail;
  598. if (TestSetPageLocked(page))
  599. goto fail;
  600. if (PageWriteback(page))
  601. goto fail_unlock_page;
  602. if (page->mapping != inode->i_mapping)
  603. goto fail_unlock_page;
  604. if (!xfs_is_delayed_page(page, (*ioendp)->io_type))
  605. goto fail_unlock_page;
  606. /*
  607. * page_dirty is initially a count of buffers on the page before
  608. * EOF and is decrememted as we move each into a cleanable state.
  609. *
  610. * Derivation:
  611. *
  612. * End offset is the highest offset that this page should represent.
  613. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  614. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  615. * hence give us the correct page_dirty count. On any other page,
  616. * it will be zero and in that case we need page_dirty to be the
  617. * count of buffers on the page.
  618. */
  619. end_offset = min_t(unsigned long long,
  620. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT,
  621. i_size_read(inode));
  622. len = 1 << inode->i_blkbits;
  623. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  624. PAGE_CACHE_SIZE);
  625. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  626. page_dirty = p_offset / len;
  627. bh = head = page_buffers(page);
  628. do {
  629. if (offset >= end_offset)
  630. break;
  631. if (!buffer_uptodate(bh))
  632. uptodate = 0;
  633. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  634. done = 1;
  635. continue;
  636. }
  637. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  638. if (buffer_unwritten(bh))
  639. type = IOMAP_UNWRITTEN;
  640. else
  641. type = IOMAP_DELAY;
  642. if (!xfs_iomap_valid(mp, offset)) {
  643. done = 1;
  644. continue;
  645. }
  646. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  647. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  648. xfs_map_at_offset(bh, offset, bbits, mp);
  649. if (startio) {
  650. xfs_add_to_ioend(inode, bh, offset,
  651. type, ioendp, done);
  652. } else {
  653. set_buffer_dirty(bh);
  654. unlock_buffer(bh);
  655. mark_buffer_dirty(bh);
  656. }
  657. page_dirty--;
  658. count++;
  659. } else {
  660. type = 0;
  661. if (buffer_mapped(bh) && all_bh && startio) {
  662. lock_buffer(bh);
  663. xfs_add_to_ioend(inode, bh, offset,
  664. type, ioendp, done);
  665. count++;
  666. page_dirty--;
  667. } else {
  668. done = 1;
  669. }
  670. }
  671. } while (offset += len, (bh = bh->b_this_page) != head);
  672. if (uptodate && bh == head)
  673. SetPageUptodate(page);
  674. if (startio) {
  675. if (count) {
  676. struct backing_dev_info *bdi;
  677. bdi = inode->i_mapping->backing_dev_info;
  678. wbc->nr_to_write--;
  679. if (bdi_write_congested(bdi)) {
  680. wbc->encountered_congestion = 1;
  681. done = 1;
  682. } else if (wbc->nr_to_write <= 0) {
  683. done = 1;
  684. }
  685. }
  686. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  687. }
  688. return done;
  689. fail_unlock_page:
  690. unlock_page(page);
  691. fail:
  692. return 1;
  693. }
  694. /*
  695. * Convert & write out a cluster of pages in the same extent as defined
  696. * by mp and following the start page.
  697. */
  698. STATIC void
  699. xfs_cluster_write(
  700. struct inode *inode,
  701. pgoff_t tindex,
  702. xfs_iomap_t *iomapp,
  703. xfs_ioend_t **ioendp,
  704. struct writeback_control *wbc,
  705. int startio,
  706. int all_bh,
  707. pgoff_t tlast)
  708. {
  709. struct pagevec pvec;
  710. int done = 0, i;
  711. pagevec_init(&pvec, 0);
  712. while (!done && tindex <= tlast) {
  713. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  714. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  715. break;
  716. for (i = 0; i < pagevec_count(&pvec); i++) {
  717. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  718. iomapp, ioendp, wbc, startio, all_bh);
  719. if (done)
  720. break;
  721. }
  722. pagevec_release(&pvec);
  723. cond_resched();
  724. }
  725. }
  726. /*
  727. * Calling this without startio set means we are being asked to make a dirty
  728. * page ready for freeing it's buffers. When called with startio set then
  729. * we are coming from writepage.
  730. *
  731. * When called with startio set it is important that we write the WHOLE
  732. * page if possible.
  733. * The bh->b_state's cannot know if any of the blocks or which block for
  734. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  735. * only vaild if the page itself isn't completely uptodate. Some layers
  736. * may clear the page dirty flag prior to calling write page, under the
  737. * assumption the entire page will be written out; by not writing out the
  738. * whole page the page can be reused before all valid dirty data is
  739. * written out. Note: in the case of a page that has been dirty'd by
  740. * mapwrite and but partially setup by block_prepare_write the
  741. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  742. * valid state, thus the whole page must be written out thing.
  743. */
  744. STATIC int
  745. xfs_page_state_convert(
  746. struct inode *inode,
  747. struct page *page,
  748. struct writeback_control *wbc,
  749. int startio,
  750. int unmapped) /* also implies page uptodate */
  751. {
  752. struct buffer_head *bh, *head;
  753. xfs_iomap_t iomap;
  754. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  755. loff_t offset;
  756. unsigned long p_offset = 0;
  757. unsigned int type;
  758. __uint64_t end_offset;
  759. pgoff_t end_index, last_index, tlast;
  760. ssize_t size, len;
  761. int flags, err, iomap_valid = 0, uptodate = 1;
  762. int page_dirty, count = 0, trylock_flag = 0;
  763. int all_bh = unmapped;
  764. /* wait for other IO threads? */
  765. if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
  766. trylock_flag |= BMAPI_TRYLOCK;
  767. /* Is this page beyond the end of the file? */
  768. offset = i_size_read(inode);
  769. end_index = offset >> PAGE_CACHE_SHIFT;
  770. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  771. if (page->index >= end_index) {
  772. if ((page->index >= end_index + 1) ||
  773. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  774. if (startio)
  775. unlock_page(page);
  776. return 0;
  777. }
  778. }
  779. /*
  780. * page_dirty is initially a count of buffers on the page before
  781. * EOF and is decrememted as we move each into a cleanable state.
  782. *
  783. * Derivation:
  784. *
  785. * End offset is the highest offset that this page should represent.
  786. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  787. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  788. * hence give us the correct page_dirty count. On any other page,
  789. * it will be zero and in that case we need page_dirty to be the
  790. * count of buffers on the page.
  791. */
  792. end_offset = min_t(unsigned long long,
  793. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  794. len = 1 << inode->i_blkbits;
  795. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  796. PAGE_CACHE_SIZE);
  797. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  798. page_dirty = p_offset / len;
  799. bh = head = page_buffers(page);
  800. offset = page_offset(page);
  801. flags = -1;
  802. type = 0;
  803. /* TODO: cleanup count and page_dirty */
  804. do {
  805. if (offset >= end_offset)
  806. break;
  807. if (!buffer_uptodate(bh))
  808. uptodate = 0;
  809. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  810. /*
  811. * the iomap is actually still valid, but the ioend
  812. * isn't. shouldn't happen too often.
  813. */
  814. iomap_valid = 0;
  815. continue;
  816. }
  817. if (iomap_valid)
  818. iomap_valid = xfs_iomap_valid(&iomap, offset);
  819. /*
  820. * First case, map an unwritten extent and prepare for
  821. * extent state conversion transaction on completion.
  822. *
  823. * Second case, allocate space for a delalloc buffer.
  824. * We can return EAGAIN here in the release page case.
  825. *
  826. * Third case, an unmapped buffer was found, and we are
  827. * in a path where we need to write the whole page out.
  828. */
  829. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  830. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  831. !buffer_mapped(bh) && (unmapped || startio))) {
  832. /*
  833. * Make sure we don't use a read-only iomap
  834. */
  835. if (flags == BMAPI_READ)
  836. iomap_valid = 0;
  837. if (buffer_unwritten(bh)) {
  838. type = IOMAP_UNWRITTEN;
  839. flags = BMAPI_WRITE|BMAPI_IGNSTATE;
  840. } else if (buffer_delay(bh)) {
  841. type = IOMAP_DELAY;
  842. flags = BMAPI_ALLOCATE;
  843. if (!startio)
  844. flags |= trylock_flag;
  845. } else {
  846. type = IOMAP_NEW;
  847. flags = BMAPI_WRITE|BMAPI_MMAP;
  848. }
  849. if (!iomap_valid) {
  850. if (type == IOMAP_NEW) {
  851. size = xfs_probe_cluster(inode,
  852. page, bh, head, 0);
  853. } else {
  854. size = len;
  855. }
  856. err = xfs_map_blocks(inode, offset, size,
  857. &iomap, flags);
  858. if (err)
  859. goto error;
  860. iomap_valid = xfs_iomap_valid(&iomap, offset);
  861. }
  862. if (iomap_valid) {
  863. xfs_map_at_offset(bh, offset,
  864. inode->i_blkbits, &iomap);
  865. if (startio) {
  866. xfs_add_to_ioend(inode, bh, offset,
  867. type, &ioend,
  868. !iomap_valid);
  869. } else {
  870. set_buffer_dirty(bh);
  871. unlock_buffer(bh);
  872. mark_buffer_dirty(bh);
  873. }
  874. page_dirty--;
  875. count++;
  876. }
  877. } else if (buffer_uptodate(bh) && startio) {
  878. /*
  879. * we got here because the buffer is already mapped.
  880. * That means it must already have extents allocated
  881. * underneath it. Map the extent by reading it.
  882. */
  883. if (!iomap_valid || type != 0) {
  884. flags = BMAPI_READ;
  885. size = xfs_probe_cluster(inode, page, bh,
  886. head, 1);
  887. err = xfs_map_blocks(inode, offset, size,
  888. &iomap, flags);
  889. if (err)
  890. goto error;
  891. iomap_valid = xfs_iomap_valid(&iomap, offset);
  892. }
  893. type = 0;
  894. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  895. ASSERT(buffer_mapped(bh));
  896. if (iomap_valid)
  897. all_bh = 1;
  898. xfs_add_to_ioend(inode, bh, offset, type,
  899. &ioend, !iomap_valid);
  900. page_dirty--;
  901. count++;
  902. } else {
  903. iomap_valid = 0;
  904. }
  905. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  906. (unmapped || startio)) {
  907. iomap_valid = 0;
  908. }
  909. if (!iohead)
  910. iohead = ioend;
  911. } while (offset += len, ((bh = bh->b_this_page) != head));
  912. if (uptodate && bh == head)
  913. SetPageUptodate(page);
  914. if (startio)
  915. xfs_start_page_writeback(page, wbc, 1, count);
  916. if (ioend && iomap_valid) {
  917. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  918. PAGE_CACHE_SHIFT;
  919. tlast = min_t(pgoff_t, offset, last_index);
  920. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  921. wbc, startio, all_bh, tlast);
  922. }
  923. if (iohead)
  924. xfs_submit_ioend(iohead);
  925. return page_dirty;
  926. error:
  927. if (iohead)
  928. xfs_cancel_ioend(iohead);
  929. /*
  930. * If it's delalloc and we have nowhere to put it,
  931. * throw it away, unless the lower layers told
  932. * us to try again.
  933. */
  934. if (err != -EAGAIN) {
  935. if (!unmapped)
  936. block_invalidatepage(page, 0);
  937. ClearPageUptodate(page);
  938. }
  939. return err;
  940. }
  941. /*
  942. * writepage: Called from one of two places:
  943. *
  944. * 1. we are flushing a delalloc buffer head.
  945. *
  946. * 2. we are writing out a dirty page. Typically the page dirty
  947. * state is cleared before we get here. In this case is it
  948. * conceivable we have no buffer heads.
  949. *
  950. * For delalloc space on the page we need to allocate space and
  951. * flush it. For unmapped buffer heads on the page we should
  952. * allocate space if the page is uptodate. For any other dirty
  953. * buffer heads on the page we should flush them.
  954. *
  955. * If we detect that a transaction would be required to flush
  956. * the page, we have to check the process flags first, if we
  957. * are already in a transaction or disk I/O during allocations
  958. * is off, we need to fail the writepage and redirty the page.
  959. */
  960. STATIC int
  961. linvfs_writepage(
  962. struct page *page,
  963. struct writeback_control *wbc)
  964. {
  965. int error;
  966. int need_trans;
  967. int delalloc, unmapped, unwritten;
  968. struct inode *inode = page->mapping->host;
  969. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  970. /*
  971. * We need a transaction if:
  972. * 1. There are delalloc buffers on the page
  973. * 2. The page is uptodate and we have unmapped buffers
  974. * 3. The page is uptodate and we have no buffers
  975. * 4. There are unwritten buffers on the page
  976. */
  977. if (!page_has_buffers(page)) {
  978. unmapped = 1;
  979. need_trans = 1;
  980. } else {
  981. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  982. if (!PageUptodate(page))
  983. unmapped = 0;
  984. need_trans = delalloc + unmapped + unwritten;
  985. }
  986. /*
  987. * If we need a transaction and the process flags say
  988. * we are already in a transaction, or no IO is allowed
  989. * then mark the page dirty again and leave the page
  990. * as is.
  991. */
  992. if (PFLAGS_TEST_FSTRANS() && need_trans)
  993. goto out_fail;
  994. /*
  995. * Delay hooking up buffer heads until we have
  996. * made our go/no-go decision.
  997. */
  998. if (!page_has_buffers(page))
  999. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1000. /*
  1001. * Convert delayed allocate, unwritten or unmapped space
  1002. * to real space and flush out to disk.
  1003. */
  1004. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1005. if (error == -EAGAIN)
  1006. goto out_fail;
  1007. if (unlikely(error < 0))
  1008. goto out_unlock;
  1009. return 0;
  1010. out_fail:
  1011. redirty_page_for_writepage(wbc, page);
  1012. unlock_page(page);
  1013. return 0;
  1014. out_unlock:
  1015. unlock_page(page);
  1016. return error;
  1017. }
  1018. /*
  1019. * Called to move a page into cleanable state - and from there
  1020. * to be released. Possibly the page is already clean. We always
  1021. * have buffer heads in this call.
  1022. *
  1023. * Returns 0 if the page is ok to release, 1 otherwise.
  1024. *
  1025. * Possible scenarios are:
  1026. *
  1027. * 1. We are being called to release a page which has been written
  1028. * to via regular I/O. buffer heads will be dirty and possibly
  1029. * delalloc. If no delalloc buffer heads in this case then we
  1030. * can just return zero.
  1031. *
  1032. * 2. We are called to release a page which has been written via
  1033. * mmap, all we need to do is ensure there is no delalloc
  1034. * state in the buffer heads, if not we can let the caller
  1035. * free them and we should come back later via writepage.
  1036. */
  1037. STATIC int
  1038. linvfs_release_page(
  1039. struct page *page,
  1040. gfp_t gfp_mask)
  1041. {
  1042. struct inode *inode = page->mapping->host;
  1043. int dirty, delalloc, unmapped, unwritten;
  1044. struct writeback_control wbc = {
  1045. .sync_mode = WB_SYNC_ALL,
  1046. .nr_to_write = 1,
  1047. };
  1048. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1049. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1050. if (!delalloc && !unwritten)
  1051. goto free_buffers;
  1052. if (!(gfp_mask & __GFP_FS))
  1053. return 0;
  1054. /* If we are already inside a transaction or the thread cannot
  1055. * do I/O, we cannot release this page.
  1056. */
  1057. if (PFLAGS_TEST_FSTRANS())
  1058. return 0;
  1059. /*
  1060. * Convert delalloc space to real space, do not flush the
  1061. * data out to disk, that will be done by the caller.
  1062. * Never need to allocate space here - we will always
  1063. * come back to writepage in that case.
  1064. */
  1065. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1066. if (dirty == 0 && !unwritten)
  1067. goto free_buffers;
  1068. return 0;
  1069. free_buffers:
  1070. return try_to_free_buffers(page);
  1071. }
  1072. STATIC int
  1073. __linvfs_get_block(
  1074. struct inode *inode,
  1075. sector_t iblock,
  1076. unsigned long blocks,
  1077. struct buffer_head *bh_result,
  1078. int create,
  1079. int direct,
  1080. bmapi_flags_t flags)
  1081. {
  1082. vnode_t *vp = LINVFS_GET_VP(inode);
  1083. xfs_iomap_t iomap;
  1084. xfs_off_t offset;
  1085. ssize_t size;
  1086. int retpbbm = 1;
  1087. int error;
  1088. offset = (xfs_off_t)iblock << inode->i_blkbits;
  1089. if (blocks)
  1090. size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
  1091. (xfs_off_t)blocks << inode->i_blkbits);
  1092. else
  1093. size = 1 << inode->i_blkbits;
  1094. VOP_BMAP(vp, offset, size,
  1095. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  1096. if (error)
  1097. return -error;
  1098. if (retpbbm == 0)
  1099. return 0;
  1100. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  1101. xfs_daddr_t bn;
  1102. xfs_off_t delta;
  1103. /* For unwritten extents do not report a disk address on
  1104. * the read case (treat as if we're reading into a hole).
  1105. */
  1106. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1107. delta = offset - iomap.iomap_offset;
  1108. delta >>= inode->i_blkbits;
  1109. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  1110. bn += delta;
  1111. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  1112. bh_result->b_blocknr = bn;
  1113. set_buffer_mapped(bh_result);
  1114. }
  1115. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  1116. if (direct)
  1117. bh_result->b_private = inode;
  1118. set_buffer_unwritten(bh_result);
  1119. set_buffer_delay(bh_result);
  1120. }
  1121. }
  1122. /* If this is a realtime file, data might be on a new device */
  1123. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  1124. /* If we previously allocated a block out beyond eof and
  1125. * we are now coming back to use it then we will need to
  1126. * flag it as new even if it has a disk address.
  1127. */
  1128. if (create &&
  1129. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  1130. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
  1131. set_buffer_new(bh_result);
  1132. if (iomap.iomap_flags & IOMAP_DELAY) {
  1133. BUG_ON(direct);
  1134. if (create) {
  1135. set_buffer_uptodate(bh_result);
  1136. set_buffer_mapped(bh_result);
  1137. set_buffer_delay(bh_result);
  1138. }
  1139. }
  1140. if (blocks) {
  1141. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  1142. offset = min_t(xfs_off_t,
  1143. iomap.iomap_bsize - iomap.iomap_delta,
  1144. (xfs_off_t)blocks << inode->i_blkbits);
  1145. bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
  1146. }
  1147. return 0;
  1148. }
  1149. int
  1150. linvfs_get_block(
  1151. struct inode *inode,
  1152. sector_t iblock,
  1153. struct buffer_head *bh_result,
  1154. int create)
  1155. {
  1156. return __linvfs_get_block(inode, iblock, 0, bh_result,
  1157. create, 0, BMAPI_WRITE);
  1158. }
  1159. STATIC int
  1160. linvfs_get_blocks_direct(
  1161. struct inode *inode,
  1162. sector_t iblock,
  1163. unsigned long max_blocks,
  1164. struct buffer_head *bh_result,
  1165. int create)
  1166. {
  1167. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  1168. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1169. }
  1170. STATIC void
  1171. linvfs_end_io_direct(
  1172. struct kiocb *iocb,
  1173. loff_t offset,
  1174. ssize_t size,
  1175. void *private)
  1176. {
  1177. xfs_ioend_t *ioend = iocb->private;
  1178. /*
  1179. * Non-NULL private data means we need to issue a transaction to
  1180. * convert a range from unwritten to written extents. This needs
  1181. * to happen from process contect but aio+dio I/O completion
  1182. * happens from irq context so we need to defer it to a workqueue.
  1183. * This is not nessecary for synchronous direct I/O, but we do
  1184. * it anyway to keep the code uniform and simpler.
  1185. *
  1186. * The core direct I/O code might be changed to always call the
  1187. * completion handler in the future, in which case all this can
  1188. * go away.
  1189. */
  1190. if (private && size > 0) {
  1191. ioend->io_offset = offset;
  1192. ioend->io_size = size;
  1193. xfs_finish_ioend(ioend);
  1194. } else {
  1195. ASSERT(size >= 0);
  1196. xfs_destroy_ioend(ioend);
  1197. }
  1198. /*
  1199. * blockdev_direct_IO can return an error even afer the I/O
  1200. * completion handler was called. Thus we need to protect
  1201. * against double-freeing.
  1202. */
  1203. iocb->private = NULL;
  1204. }
  1205. STATIC ssize_t
  1206. linvfs_direct_IO(
  1207. int rw,
  1208. struct kiocb *iocb,
  1209. const struct iovec *iov,
  1210. loff_t offset,
  1211. unsigned long nr_segs)
  1212. {
  1213. struct file *file = iocb->ki_filp;
  1214. struct inode *inode = file->f_mapping->host;
  1215. vnode_t *vp = LINVFS_GET_VP(inode);
  1216. xfs_iomap_t iomap;
  1217. int maps = 1;
  1218. int error;
  1219. ssize_t ret;
  1220. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1221. if (error)
  1222. return -error;
  1223. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1224. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1225. iomap.iomap_target->bt_bdev,
  1226. iov, offset, nr_segs,
  1227. linvfs_get_blocks_direct,
  1228. linvfs_end_io_direct);
  1229. if (unlikely(ret <= 0 && iocb->private))
  1230. xfs_destroy_ioend(iocb->private);
  1231. return ret;
  1232. }
  1233. STATIC int
  1234. linvfs_prepare_write(
  1235. struct file *file,
  1236. struct page *page,
  1237. unsigned int from,
  1238. unsigned int to)
  1239. {
  1240. return block_prepare_write(page, from, to, linvfs_get_block);
  1241. }
  1242. STATIC sector_t
  1243. linvfs_bmap(
  1244. struct address_space *mapping,
  1245. sector_t block)
  1246. {
  1247. struct inode *inode = (struct inode *)mapping->host;
  1248. vnode_t *vp = LINVFS_GET_VP(inode);
  1249. int error;
  1250. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1251. VOP_RWLOCK(vp, VRWLOCK_READ);
  1252. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1253. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1254. return generic_block_bmap(mapping, block, linvfs_get_block);
  1255. }
  1256. STATIC int
  1257. linvfs_readpage(
  1258. struct file *unused,
  1259. struct page *page)
  1260. {
  1261. return mpage_readpage(page, linvfs_get_block);
  1262. }
  1263. STATIC int
  1264. linvfs_readpages(
  1265. struct file *unused,
  1266. struct address_space *mapping,
  1267. struct list_head *pages,
  1268. unsigned nr_pages)
  1269. {
  1270. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1271. }
  1272. STATIC int
  1273. linvfs_invalidate_page(
  1274. struct page *page,
  1275. unsigned long offset)
  1276. {
  1277. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1278. page->mapping->host, page, offset);
  1279. return block_invalidatepage(page, offset);
  1280. }
  1281. struct address_space_operations linvfs_aops = {
  1282. .readpage = linvfs_readpage,
  1283. .readpages = linvfs_readpages,
  1284. .writepage = linvfs_writepage,
  1285. .sync_page = block_sync_page,
  1286. .releasepage = linvfs_release_page,
  1287. .invalidatepage = linvfs_invalidate_page,
  1288. .prepare_write = linvfs_prepare_write,
  1289. .commit_write = generic_commit_write,
  1290. .bmap = linvfs_bmap,
  1291. .direct_IO = linvfs_direct_IO,
  1292. .migratepage = buffer_migrate_page,
  1293. };