xfs_aops.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442
  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,
  301. * covering the initial writepage page and also any probed pages.
  302. */
  303. STATIC void
  304. xfs_submit_ioend(
  305. xfs_ioend_t *ioend)
  306. {
  307. xfs_ioend_t *next;
  308. struct buffer_head *bh;
  309. struct bio *bio;
  310. sector_t lastblock = 0;
  311. do {
  312. next = ioend->io_list;
  313. bio = NULL;
  314. for (bh = ioend->io_buffer_head; bh; bh = bh->b_private) {
  315. xfs_start_buffer_writeback(bh);
  316. if (!bio) {
  317. retry:
  318. bio = xfs_alloc_ioend_bio(bh);
  319. } else if (bh->b_blocknr != lastblock + 1) {
  320. xfs_submit_ioend_bio(ioend, bio);
  321. goto retry;
  322. }
  323. if (bio_add_buffer(bio, bh) != bh->b_size) {
  324. xfs_submit_ioend_bio(ioend, bio);
  325. goto retry;
  326. }
  327. lastblock = bh->b_blocknr;
  328. }
  329. if (bio)
  330. xfs_submit_ioend_bio(ioend, bio);
  331. xfs_finish_ioend(ioend);
  332. } while ((ioend = next) != NULL);
  333. }
  334. /*
  335. * Cancel submission of all buffer_heads so far in this endio.
  336. * Toss the endio too. Only ever called for the initial page
  337. * in a writepage request, so only ever one page.
  338. */
  339. STATIC void
  340. xfs_cancel_ioend(
  341. xfs_ioend_t *ioend)
  342. {
  343. xfs_ioend_t *next;
  344. struct buffer_head *bh, *next_bh;
  345. do {
  346. next = ioend->io_list;
  347. bh = ioend->io_buffer_head;
  348. do {
  349. next_bh = bh->b_private;
  350. clear_buffer_async_write(bh);
  351. unlock_buffer(bh);
  352. } while ((bh = next_bh) != NULL);
  353. vn_iowake(ioend->io_vnode);
  354. mempool_free(ioend, xfs_ioend_pool);
  355. } while ((ioend = next) != NULL);
  356. }
  357. /*
  358. * Test to see if we've been building up a completion structure for
  359. * earlier buffers -- if so, we try to append to this ioend if we
  360. * can, otherwise we finish off any current ioend and start another.
  361. * Return true if we've finished the given ioend.
  362. */
  363. STATIC void
  364. xfs_add_to_ioend(
  365. struct inode *inode,
  366. struct buffer_head *bh,
  367. xfs_off_t offset,
  368. unsigned int type,
  369. xfs_ioend_t **result,
  370. int need_ioend)
  371. {
  372. xfs_ioend_t *ioend = *result;
  373. if (!ioend || need_ioend || type != ioend->io_type) {
  374. xfs_ioend_t *previous = *result;
  375. ioend = xfs_alloc_ioend(inode, type);
  376. ioend->io_offset = offset;
  377. ioend->io_buffer_head = bh;
  378. ioend->io_buffer_tail = bh;
  379. if (previous)
  380. previous->io_list = ioend;
  381. *result = ioend;
  382. } else {
  383. ioend->io_buffer_tail->b_private = bh;
  384. ioend->io_buffer_tail = bh;
  385. }
  386. bh->b_private = NULL;
  387. ioend->io_size += bh->b_size;
  388. }
  389. STATIC void
  390. xfs_map_at_offset(
  391. struct buffer_head *bh,
  392. loff_t offset,
  393. int block_bits,
  394. xfs_iomap_t *iomapp)
  395. {
  396. xfs_daddr_t bn;
  397. int sector_shift;
  398. ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
  399. ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
  400. ASSERT(iomapp->iomap_bn != IOMAP_DADDR_NULL);
  401. sector_shift = block_bits - BBSHIFT;
  402. bn = (iomapp->iomap_bn >> sector_shift) +
  403. ((offset - iomapp->iomap_offset) >> block_bits);
  404. ASSERT(bn || (iomapp->iomap_flags & IOMAP_REALTIME));
  405. ASSERT((bn << sector_shift) >= iomapp->iomap_bn);
  406. lock_buffer(bh);
  407. bh->b_blocknr = bn;
  408. bh->b_bdev = iomapp->iomap_target->bt_bdev;
  409. set_buffer_mapped(bh);
  410. clear_buffer_delay(bh);
  411. clear_buffer_unwritten(bh);
  412. }
  413. /*
  414. * Look for a page at index that is suitable for clustering.
  415. */
  416. STATIC unsigned int
  417. xfs_probe_page(
  418. struct page *page,
  419. unsigned int pg_offset,
  420. int mapped)
  421. {
  422. int ret = 0;
  423. if (PageWriteback(page))
  424. return 0;
  425. if (page->mapping && PageDirty(page)) {
  426. if (page_has_buffers(page)) {
  427. struct buffer_head *bh, *head;
  428. bh = head = page_buffers(page);
  429. do {
  430. if (!buffer_uptodate(bh))
  431. break;
  432. if (mapped != buffer_mapped(bh))
  433. break;
  434. ret += bh->b_size;
  435. if (ret >= pg_offset)
  436. break;
  437. } while ((bh = bh->b_this_page) != head);
  438. } else
  439. ret = mapped ? 0 : PAGE_CACHE_SIZE;
  440. }
  441. return ret;
  442. }
  443. STATIC size_t
  444. xfs_probe_cluster(
  445. struct inode *inode,
  446. struct page *startpage,
  447. struct buffer_head *bh,
  448. struct buffer_head *head,
  449. int mapped)
  450. {
  451. struct pagevec pvec;
  452. pgoff_t tindex, tlast, tloff;
  453. size_t total = 0;
  454. int done = 0, i;
  455. /* First sum forwards in this page */
  456. do {
  457. if (mapped != buffer_mapped(bh))
  458. return total;
  459. total += bh->b_size;
  460. } while ((bh = bh->b_this_page) != head);
  461. /* if we reached the end of the page, sum forwards in following pages */
  462. tlast = i_size_read(inode) >> PAGE_CACHE_SHIFT;
  463. tindex = startpage->index + 1;
  464. /* Prune this back to avoid pathological behavior */
  465. tloff = min(tlast, startpage->index + 64);
  466. pagevec_init(&pvec, 0);
  467. while (!done && tindex <= tloff) {
  468. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  469. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  470. break;
  471. for (i = 0; i < pagevec_count(&pvec); i++) {
  472. struct page *page = pvec.pages[i];
  473. size_t pg_offset, len = 0;
  474. if (tindex == tlast) {
  475. pg_offset =
  476. i_size_read(inode) & (PAGE_CACHE_SIZE - 1);
  477. if (!pg_offset) {
  478. done = 1;
  479. break;
  480. }
  481. } else
  482. pg_offset = PAGE_CACHE_SIZE;
  483. if (page->index == tindex && !TestSetPageLocked(page)) {
  484. len = xfs_probe_page(page, pg_offset, mapped);
  485. unlock_page(page);
  486. }
  487. if (!len) {
  488. done = 1;
  489. break;
  490. }
  491. total += len;
  492. tindex++;
  493. }
  494. pagevec_release(&pvec);
  495. cond_resched();
  496. }
  497. return total;
  498. }
  499. /*
  500. * Test if a given page is suitable for writing as part of an unwritten
  501. * or delayed allocate extent.
  502. */
  503. STATIC int
  504. xfs_is_delayed_page(
  505. struct page *page,
  506. unsigned int type)
  507. {
  508. if (PageWriteback(page))
  509. return 0;
  510. if (page->mapping && page_has_buffers(page)) {
  511. struct buffer_head *bh, *head;
  512. int acceptable = 0;
  513. bh = head = page_buffers(page);
  514. do {
  515. if (buffer_unwritten(bh))
  516. acceptable = (type == IOMAP_UNWRITTEN);
  517. else if (buffer_delay(bh))
  518. acceptable = (type == IOMAP_DELAY);
  519. else if (buffer_mapped(bh))
  520. acceptable = (type == 0);
  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. bh = head = page_buffers(page);
  586. do {
  587. if (offset >= end_offset)
  588. break;
  589. if (!buffer_uptodate(bh))
  590. uptodate = 0;
  591. if (!(PageUptodate(page) || buffer_uptodate(bh))) {
  592. done = 1;
  593. continue;
  594. }
  595. if (buffer_unwritten(bh) || buffer_delay(bh)) {
  596. if (buffer_unwritten(bh))
  597. type = IOMAP_UNWRITTEN;
  598. else
  599. type = IOMAP_DELAY;
  600. if (!xfs_iomap_valid(mp, offset)) {
  601. done = 1;
  602. continue;
  603. }
  604. ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
  605. ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
  606. xfs_map_at_offset(bh, offset, bbits, mp);
  607. if (startio) {
  608. xfs_add_to_ioend(inode, bh, offset,
  609. type, ioendp, done);
  610. } else {
  611. set_buffer_dirty(bh);
  612. unlock_buffer(bh);
  613. mark_buffer_dirty(bh);
  614. }
  615. page_dirty--;
  616. count++;
  617. } else {
  618. type = 0;
  619. if (buffer_mapped(bh) && all_bh && startio) {
  620. lock_buffer(bh);
  621. xfs_add_to_ioend(inode, bh, offset,
  622. type, ioendp, done);
  623. count++;
  624. page_dirty--;
  625. } else {
  626. done = 1;
  627. }
  628. }
  629. } while (offset += len, (bh = bh->b_this_page) != head);
  630. if (uptodate && bh == head)
  631. SetPageUptodate(page);
  632. if (startio) {
  633. if (count) {
  634. struct backing_dev_info *bdi;
  635. bdi = inode->i_mapping->backing_dev_info;
  636. if (bdi_write_congested(bdi)) {
  637. wbc->encountered_congestion = 1;
  638. done = 1;
  639. } else if (--wbc->nr_to_write <= 0) {
  640. done = 1;
  641. }
  642. }
  643. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  644. }
  645. return done;
  646. fail_unlock_page:
  647. unlock_page(page);
  648. fail:
  649. return 1;
  650. }
  651. /*
  652. * Convert & write out a cluster of pages in the same extent as defined
  653. * by mp and following the start page.
  654. */
  655. STATIC void
  656. xfs_cluster_write(
  657. struct inode *inode,
  658. pgoff_t tindex,
  659. xfs_iomap_t *iomapp,
  660. xfs_ioend_t **ioendp,
  661. struct writeback_control *wbc,
  662. int startio,
  663. int all_bh,
  664. pgoff_t tlast)
  665. {
  666. struct pagevec pvec;
  667. int done = 0, i;
  668. pagevec_init(&pvec, 0);
  669. while (!done && tindex <= tlast) {
  670. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  671. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  672. break;
  673. for (i = 0; i < pagevec_count(&pvec); i++) {
  674. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  675. iomapp, ioendp, wbc, startio, all_bh);
  676. if (done)
  677. break;
  678. }
  679. pagevec_release(&pvec);
  680. cond_resched();
  681. }
  682. }
  683. /*
  684. * Calling this without startio set means we are being asked to make a dirty
  685. * page ready for freeing it's buffers. When called with startio set then
  686. * we are coming from writepage.
  687. *
  688. * When called with startio set it is important that we write the WHOLE
  689. * page if possible.
  690. * The bh->b_state's cannot know if any of the blocks or which block for
  691. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  692. * only vaild if the page itself isn't completely uptodate. Some layers
  693. * may clear the page dirty flag prior to calling write page, under the
  694. * assumption the entire page will be written out; by not writing out the
  695. * whole page the page can be reused before all valid dirty data is
  696. * written out. Note: in the case of a page that has been dirty'd by
  697. * mapwrite and but partially setup by block_prepare_write the
  698. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  699. * valid state, thus the whole page must be written out thing.
  700. */
  701. STATIC int
  702. xfs_page_state_convert(
  703. struct inode *inode,
  704. struct page *page,
  705. struct writeback_control *wbc,
  706. int startio,
  707. int unmapped) /* also implies page uptodate */
  708. {
  709. struct buffer_head *bh, *head;
  710. xfs_iomap_t iomap;
  711. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  712. loff_t offset;
  713. unsigned long p_offset = 0;
  714. unsigned int type;
  715. __uint64_t end_offset;
  716. pgoff_t end_index, last_index, tlast;
  717. ssize_t size, len;
  718. int flags, err, iomap_valid = 0, uptodate = 1;
  719. int page_dirty, count = 0, trylock_flag = 0;
  720. int all_bh = unmapped;
  721. /* wait for other IO threads? */
  722. if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
  723. trylock_flag |= BMAPI_TRYLOCK;
  724. /* Is this page beyond the end of the file? */
  725. offset = i_size_read(inode);
  726. end_index = offset >> PAGE_CACHE_SHIFT;
  727. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  728. if (page->index >= end_index) {
  729. if ((page->index >= end_index + 1) ||
  730. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  731. if (startio)
  732. unlock_page(page);
  733. return 0;
  734. }
  735. }
  736. /*
  737. * page_dirty is initially a count of buffers on the page before
  738. * EOF and is decrememted as we move each into a cleanable state.
  739. *
  740. * Derivation:
  741. *
  742. * End offset is the highest offset that this page should represent.
  743. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  744. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  745. * hence give us the correct page_dirty count. On any other page,
  746. * it will be zero and in that case we need page_dirty to be the
  747. * count of buffers on the page.
  748. */
  749. end_offset = min_t(unsigned long long,
  750. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  751. len = 1 << inode->i_blkbits;
  752. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  753. PAGE_CACHE_SIZE);
  754. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  755. page_dirty = p_offset / len;
  756. bh = head = page_buffers(page);
  757. offset = page_offset(page);
  758. flags = -1;
  759. type = 0;
  760. /* TODO: cleanup count and page_dirty */
  761. do {
  762. if (offset >= end_offset)
  763. break;
  764. if (!buffer_uptodate(bh))
  765. uptodate = 0;
  766. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  767. /*
  768. * the iomap is actually still valid, but the ioend
  769. * isn't. shouldn't happen too often.
  770. */
  771. iomap_valid = 0;
  772. continue;
  773. }
  774. if (iomap_valid)
  775. iomap_valid = xfs_iomap_valid(&iomap, offset);
  776. /*
  777. * First case, map an unwritten extent and prepare for
  778. * extent state conversion transaction on completion.
  779. *
  780. * Second case, allocate space for a delalloc buffer.
  781. * We can return EAGAIN here in the release page case.
  782. *
  783. * Third case, an unmapped buffer was found, and we are
  784. * in a path where we need to write the whole page out.
  785. */
  786. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  787. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  788. !buffer_mapped(bh) && (unmapped || startio))) {
  789. /*
  790. * Make sure we don't use a read-only iomap
  791. */
  792. if (flags == BMAPI_READ)
  793. iomap_valid = 0;
  794. if (buffer_unwritten(bh)) {
  795. type = IOMAP_UNWRITTEN;
  796. flags = BMAPI_WRITE|BMAPI_IGNSTATE;
  797. } else if (buffer_delay(bh)) {
  798. type = IOMAP_DELAY;
  799. flags = BMAPI_ALLOCATE;
  800. if (!startio)
  801. flags |= trylock_flag;
  802. } else {
  803. type = IOMAP_NEW;
  804. flags = BMAPI_WRITE|BMAPI_MMAP;
  805. }
  806. if (!iomap_valid) {
  807. if (type == IOMAP_NEW) {
  808. size = xfs_probe_cluster(inode,
  809. page, bh, head, 0);
  810. } else {
  811. size = len;
  812. }
  813. err = xfs_map_blocks(inode, offset, size,
  814. &iomap, flags);
  815. if (err)
  816. goto error;
  817. iomap_valid = xfs_iomap_valid(&iomap, offset);
  818. }
  819. if (iomap_valid) {
  820. xfs_map_at_offset(bh, offset,
  821. inode->i_blkbits, &iomap);
  822. if (startio) {
  823. xfs_add_to_ioend(inode, bh, offset,
  824. type, &ioend,
  825. !iomap_valid);
  826. } else {
  827. set_buffer_dirty(bh);
  828. unlock_buffer(bh);
  829. mark_buffer_dirty(bh);
  830. }
  831. page_dirty--;
  832. count++;
  833. }
  834. } else if (buffer_uptodate(bh) && startio) {
  835. /*
  836. * we got here because the buffer is already mapped.
  837. * That means it must already have extents allocated
  838. * underneath it. Map the extent by reading it.
  839. */
  840. if (!iomap_valid || type != 0) {
  841. flags = BMAPI_READ;
  842. size = xfs_probe_cluster(inode, page, bh,
  843. head, 1);
  844. err = xfs_map_blocks(inode, offset, size,
  845. &iomap, flags);
  846. if (err)
  847. goto error;
  848. iomap_valid = xfs_iomap_valid(&iomap, offset);
  849. }
  850. type = 0;
  851. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  852. ASSERT(buffer_mapped(bh));
  853. if (iomap_valid)
  854. all_bh = 1;
  855. xfs_add_to_ioend(inode, bh, offset, type,
  856. &ioend, !iomap_valid);
  857. page_dirty--;
  858. count++;
  859. } else {
  860. iomap_valid = 0;
  861. }
  862. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  863. (unmapped || startio)) {
  864. iomap_valid = 0;
  865. }
  866. if (!iohead)
  867. iohead = ioend;
  868. } while (offset += len, ((bh = bh->b_this_page) != head));
  869. if (uptodate && bh == head)
  870. SetPageUptodate(page);
  871. if (startio)
  872. xfs_start_page_writeback(page, wbc, 1, count);
  873. if (ioend && iomap_valid) {
  874. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  875. PAGE_CACHE_SHIFT;
  876. tlast = min_t(pgoff_t, offset, last_index);
  877. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  878. wbc, startio, all_bh, tlast);
  879. }
  880. if (iohead)
  881. xfs_submit_ioend(iohead);
  882. return page_dirty;
  883. error:
  884. if (iohead)
  885. xfs_cancel_ioend(iohead);
  886. /*
  887. * If it's delalloc and we have nowhere to put it,
  888. * throw it away, unless the lower layers told
  889. * us to try again.
  890. */
  891. if (err != -EAGAIN) {
  892. if (!unmapped)
  893. block_invalidatepage(page, 0);
  894. ClearPageUptodate(page);
  895. }
  896. return err;
  897. }
  898. STATIC int
  899. __linvfs_get_block(
  900. struct inode *inode,
  901. sector_t iblock,
  902. unsigned long blocks,
  903. struct buffer_head *bh_result,
  904. int create,
  905. int direct,
  906. bmapi_flags_t flags)
  907. {
  908. vnode_t *vp = LINVFS_GET_VP(inode);
  909. xfs_iomap_t iomap;
  910. xfs_off_t offset;
  911. ssize_t size;
  912. int retpbbm = 1;
  913. int error;
  914. offset = (xfs_off_t)iblock << inode->i_blkbits;
  915. if (blocks)
  916. size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
  917. (xfs_off_t)blocks << inode->i_blkbits);
  918. else
  919. size = 1 << inode->i_blkbits;
  920. VOP_BMAP(vp, offset, size,
  921. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  922. if (error)
  923. return -error;
  924. if (retpbbm == 0)
  925. return 0;
  926. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  927. xfs_daddr_t bn;
  928. xfs_off_t delta;
  929. /* For unwritten extents do not report a disk address on
  930. * the read case (treat as if we're reading into a hole).
  931. */
  932. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  933. delta = offset - iomap.iomap_offset;
  934. delta >>= inode->i_blkbits;
  935. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  936. bn += delta;
  937. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  938. bh_result->b_blocknr = bn;
  939. set_buffer_mapped(bh_result);
  940. }
  941. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  942. if (direct)
  943. bh_result->b_private = inode;
  944. set_buffer_unwritten(bh_result);
  945. set_buffer_delay(bh_result);
  946. }
  947. }
  948. /* If this is a realtime file, data might be on a new device */
  949. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  950. /* If we previously allocated a block out beyond eof and
  951. * we are now coming back to use it then we will need to
  952. * flag it as new even if it has a disk address.
  953. */
  954. if (create &&
  955. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  956. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
  957. set_buffer_new(bh_result);
  958. if (iomap.iomap_flags & IOMAP_DELAY) {
  959. BUG_ON(direct);
  960. if (create) {
  961. set_buffer_uptodate(bh_result);
  962. set_buffer_mapped(bh_result);
  963. set_buffer_delay(bh_result);
  964. }
  965. }
  966. if (blocks) {
  967. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  968. offset = min_t(xfs_off_t,
  969. iomap.iomap_bsize - iomap.iomap_delta,
  970. (xfs_off_t)blocks << inode->i_blkbits);
  971. bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
  972. }
  973. return 0;
  974. }
  975. int
  976. linvfs_get_block(
  977. struct inode *inode,
  978. sector_t iblock,
  979. struct buffer_head *bh_result,
  980. int create)
  981. {
  982. return __linvfs_get_block(inode, iblock, 0, bh_result,
  983. create, 0, BMAPI_WRITE);
  984. }
  985. STATIC int
  986. linvfs_get_blocks_direct(
  987. struct inode *inode,
  988. sector_t iblock,
  989. unsigned long max_blocks,
  990. struct buffer_head *bh_result,
  991. int create)
  992. {
  993. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  994. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  995. }
  996. STATIC void
  997. linvfs_end_io_direct(
  998. struct kiocb *iocb,
  999. loff_t offset,
  1000. ssize_t size,
  1001. void *private)
  1002. {
  1003. xfs_ioend_t *ioend = iocb->private;
  1004. /*
  1005. * Non-NULL private data means we need to issue a transaction to
  1006. * convert a range from unwritten to written extents. This needs
  1007. * to happen from process contect but aio+dio I/O completion
  1008. * happens from irq context so we need to defer it to a workqueue.
  1009. * This is not nessecary for synchronous direct I/O, but we do
  1010. * it anyway to keep the code uniform and simpler.
  1011. *
  1012. * The core direct I/O code might be changed to always call the
  1013. * completion handler in the future, in which case all this can
  1014. * go away.
  1015. */
  1016. if (private && size > 0) {
  1017. ioend->io_offset = offset;
  1018. ioend->io_size = size;
  1019. xfs_finish_ioend(ioend);
  1020. } else {
  1021. ASSERT(size >= 0);
  1022. xfs_destroy_ioend(ioend);
  1023. }
  1024. /*
  1025. * blockdev_direct_IO can return an error even afer the I/O
  1026. * completion handler was called. Thus we need to protect
  1027. * against double-freeing.
  1028. */
  1029. iocb->private = NULL;
  1030. }
  1031. STATIC ssize_t
  1032. linvfs_direct_IO(
  1033. int rw,
  1034. struct kiocb *iocb,
  1035. const struct iovec *iov,
  1036. loff_t offset,
  1037. unsigned long nr_segs)
  1038. {
  1039. struct file *file = iocb->ki_filp;
  1040. struct inode *inode = file->f_mapping->host;
  1041. vnode_t *vp = LINVFS_GET_VP(inode);
  1042. xfs_iomap_t iomap;
  1043. int maps = 1;
  1044. int error;
  1045. ssize_t ret;
  1046. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1047. if (error)
  1048. return -error;
  1049. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1050. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1051. iomap.iomap_target->bt_bdev,
  1052. iov, offset, nr_segs,
  1053. linvfs_get_blocks_direct,
  1054. linvfs_end_io_direct);
  1055. if (unlikely(ret <= 0 && iocb->private))
  1056. xfs_destroy_ioend(iocb->private);
  1057. return ret;
  1058. }
  1059. STATIC sector_t
  1060. linvfs_bmap(
  1061. struct address_space *mapping,
  1062. sector_t block)
  1063. {
  1064. struct inode *inode = (struct inode *)mapping->host;
  1065. vnode_t *vp = LINVFS_GET_VP(inode);
  1066. int error;
  1067. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1068. VOP_RWLOCK(vp, VRWLOCK_READ);
  1069. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1070. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1071. return generic_block_bmap(mapping, block, linvfs_get_block);
  1072. }
  1073. STATIC int
  1074. linvfs_readpage(
  1075. struct file *unused,
  1076. struct page *page)
  1077. {
  1078. return mpage_readpage(page, linvfs_get_block);
  1079. }
  1080. STATIC int
  1081. linvfs_readpages(
  1082. struct file *unused,
  1083. struct address_space *mapping,
  1084. struct list_head *pages,
  1085. unsigned nr_pages)
  1086. {
  1087. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1088. }
  1089. STATIC void
  1090. xfs_count_page_state(
  1091. struct page *page,
  1092. int *delalloc,
  1093. int *unmapped,
  1094. int *unwritten)
  1095. {
  1096. struct buffer_head *bh, *head;
  1097. *delalloc = *unmapped = *unwritten = 0;
  1098. bh = head = page_buffers(page);
  1099. do {
  1100. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  1101. (*unmapped) = 1;
  1102. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  1103. clear_buffer_unwritten(bh);
  1104. else if (buffer_unwritten(bh))
  1105. (*unwritten) = 1;
  1106. else if (buffer_delay(bh))
  1107. (*delalloc) = 1;
  1108. } while ((bh = bh->b_this_page) != head);
  1109. }
  1110. /*
  1111. * writepage: Called from one of two places:
  1112. *
  1113. * 1. we are flushing a delalloc buffer head.
  1114. *
  1115. * 2. we are writing out a dirty page. Typically the page dirty
  1116. * state is cleared before we get here. In this case is it
  1117. * conceivable we have no buffer heads.
  1118. *
  1119. * For delalloc space on the page we need to allocate space and
  1120. * flush it. For unmapped buffer heads on the page we should
  1121. * allocate space if the page is uptodate. For any other dirty
  1122. * buffer heads on the page we should flush them.
  1123. *
  1124. * If we detect that a transaction would be required to flush
  1125. * the page, we have to check the process flags first, if we
  1126. * are already in a transaction or disk I/O during allocations
  1127. * is off, we need to fail the writepage and redirty the page.
  1128. */
  1129. STATIC int
  1130. linvfs_writepage(
  1131. struct page *page,
  1132. struct writeback_control *wbc)
  1133. {
  1134. int error;
  1135. int need_trans;
  1136. int delalloc, unmapped, unwritten;
  1137. struct inode *inode = page->mapping->host;
  1138. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1139. /*
  1140. * We need a transaction if:
  1141. * 1. There are delalloc buffers on the page
  1142. * 2. The page is uptodate and we have unmapped buffers
  1143. * 3. The page is uptodate and we have no buffers
  1144. * 4. There are unwritten buffers on the page
  1145. */
  1146. if (!page_has_buffers(page)) {
  1147. unmapped = 1;
  1148. need_trans = 1;
  1149. } else {
  1150. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1151. if (!PageUptodate(page))
  1152. unmapped = 0;
  1153. need_trans = delalloc + unmapped + unwritten;
  1154. }
  1155. /*
  1156. * If we need a transaction and the process flags say
  1157. * we are already in a transaction, or no IO is allowed
  1158. * then mark the page dirty again and leave the page
  1159. * as is.
  1160. */
  1161. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1162. goto out_fail;
  1163. /*
  1164. * Delay hooking up buffer heads until we have
  1165. * made our go/no-go decision.
  1166. */
  1167. if (!page_has_buffers(page))
  1168. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1169. /*
  1170. * Convert delayed allocate, unwritten or unmapped space
  1171. * to real space and flush out to disk.
  1172. */
  1173. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1174. if (error == -EAGAIN)
  1175. goto out_fail;
  1176. if (unlikely(error < 0))
  1177. goto out_unlock;
  1178. return 0;
  1179. out_fail:
  1180. redirty_page_for_writepage(wbc, page);
  1181. unlock_page(page);
  1182. return 0;
  1183. out_unlock:
  1184. unlock_page(page);
  1185. return error;
  1186. }
  1187. STATIC int
  1188. linvfs_invalidate_page(
  1189. struct page *page,
  1190. unsigned long offset)
  1191. {
  1192. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1193. page->mapping->host, page, offset);
  1194. return block_invalidatepage(page, offset);
  1195. }
  1196. /*
  1197. * Called to move a page into cleanable state - and from there
  1198. * to be released. Possibly the page is already clean. We always
  1199. * have buffer heads in this call.
  1200. *
  1201. * Returns 0 if the page is ok to release, 1 otherwise.
  1202. *
  1203. * Possible scenarios are:
  1204. *
  1205. * 1. We are being called to release a page which has been written
  1206. * to via regular I/O. buffer heads will be dirty and possibly
  1207. * delalloc. If no delalloc buffer heads in this case then we
  1208. * can just return zero.
  1209. *
  1210. * 2. We are called to release a page which has been written via
  1211. * mmap, all we need to do is ensure there is no delalloc
  1212. * state in the buffer heads, if not we can let the caller
  1213. * free them and we should come back later via writepage.
  1214. */
  1215. STATIC int
  1216. linvfs_release_page(
  1217. struct page *page,
  1218. gfp_t gfp_mask)
  1219. {
  1220. struct inode *inode = page->mapping->host;
  1221. int dirty, delalloc, unmapped, unwritten;
  1222. struct writeback_control wbc = {
  1223. .sync_mode = WB_SYNC_ALL,
  1224. .nr_to_write = 1,
  1225. };
  1226. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1227. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1228. if (!delalloc && !unwritten)
  1229. goto free_buffers;
  1230. if (!(gfp_mask & __GFP_FS))
  1231. return 0;
  1232. /* If we are already inside a transaction or the thread cannot
  1233. * do I/O, we cannot release this page.
  1234. */
  1235. if (PFLAGS_TEST_FSTRANS())
  1236. return 0;
  1237. /*
  1238. * Convert delalloc space to real space, do not flush the
  1239. * data out to disk, that will be done by the caller.
  1240. * Never need to allocate space here - we will always
  1241. * come back to writepage in that case.
  1242. */
  1243. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1244. if (dirty == 0 && !unwritten)
  1245. goto free_buffers;
  1246. return 0;
  1247. free_buffers:
  1248. return try_to_free_buffers(page);
  1249. }
  1250. STATIC int
  1251. linvfs_prepare_write(
  1252. struct file *file,
  1253. struct page *page,
  1254. unsigned int from,
  1255. unsigned int to)
  1256. {
  1257. return block_prepare_write(page, from, to, linvfs_get_block);
  1258. }
  1259. struct address_space_operations linvfs_aops = {
  1260. .readpage = linvfs_readpage,
  1261. .readpages = linvfs_readpages,
  1262. .writepage = linvfs_writepage,
  1263. .sync_page = block_sync_page,
  1264. .releasepage = linvfs_release_page,
  1265. .invalidatepage = linvfs_invalidate_page,
  1266. .prepare_write = linvfs_prepare_write,
  1267. .commit_write = generic_commit_write,
  1268. .bmap = linvfs_bmap,
  1269. .direct_IO = linvfs_direct_IO,
  1270. };