xfs_aops.c 39 KB

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