xfs_aops.c 40 KB

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