xfs_aops.c 37 KB

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