xfs_aops.c 34 KB

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