xfs_aops.c 39 KB

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