xfs_aops.c 39 KB

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