xfs_aops.c 38 KB

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