xfs_aops.c 39 KB

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