xfs_aops.c 40 KB

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