xfs_aops.c 39 KB

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