xfs_aops.c 39 KB

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