xfs_aops.c 40 KB

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