xfs_aops.c 35 KB

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