xfs_aops.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466
  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 (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. if (bdi_write_congested(bdi)) {
  659. wbc->encountered_congestion = 1;
  660. done = 1;
  661. } else if (--wbc->nr_to_write <= 0) {
  662. done = 1;
  663. }
  664. }
  665. xfs_start_page_writeback(page, wbc, !page_dirty, count);
  666. }
  667. return done;
  668. fail_unlock_page:
  669. unlock_page(page);
  670. fail:
  671. return 1;
  672. }
  673. /*
  674. * Convert & write out a cluster of pages in the same extent as defined
  675. * by mp and following the start page.
  676. */
  677. STATIC void
  678. xfs_cluster_write(
  679. struct inode *inode,
  680. pgoff_t tindex,
  681. xfs_iomap_t *iomapp,
  682. xfs_ioend_t **ioendp,
  683. struct writeback_control *wbc,
  684. int startio,
  685. int all_bh,
  686. pgoff_t tlast)
  687. {
  688. struct pagevec pvec;
  689. int done = 0, i;
  690. pagevec_init(&pvec, 0);
  691. while (!done && tindex <= tlast) {
  692. unsigned len = min_t(pgoff_t, PAGEVEC_SIZE, tlast - tindex + 1);
  693. if (!pagevec_lookup(&pvec, inode->i_mapping, tindex, len))
  694. break;
  695. for (i = 0; i < pagevec_count(&pvec); i++) {
  696. done = xfs_convert_page(inode, pvec.pages[i], tindex++,
  697. iomapp, ioendp, wbc, startio, all_bh);
  698. if (done)
  699. break;
  700. }
  701. pagevec_release(&pvec);
  702. cond_resched();
  703. }
  704. }
  705. /*
  706. * Calling this without startio set means we are being asked to make a dirty
  707. * page ready for freeing it's buffers. When called with startio set then
  708. * we are coming from writepage.
  709. *
  710. * When called with startio set it is important that we write the WHOLE
  711. * page if possible.
  712. * The bh->b_state's cannot know if any of the blocks or which block for
  713. * that matter are dirty due to mmap writes, and therefore bh uptodate is
  714. * only vaild if the page itself isn't completely uptodate. Some layers
  715. * may clear the page dirty flag prior to calling write page, under the
  716. * assumption the entire page will be written out; by not writing out the
  717. * whole page the page can be reused before all valid dirty data is
  718. * written out. Note: in the case of a page that has been dirty'd by
  719. * mapwrite and but partially setup by block_prepare_write the
  720. * bh->b_states's will not agree and only ones setup by BPW/BCW will have
  721. * valid state, thus the whole page must be written out thing.
  722. */
  723. STATIC int
  724. xfs_page_state_convert(
  725. struct inode *inode,
  726. struct page *page,
  727. struct writeback_control *wbc,
  728. int startio,
  729. int unmapped) /* also implies page uptodate */
  730. {
  731. struct buffer_head *bh, *head;
  732. xfs_iomap_t iomap;
  733. xfs_ioend_t *ioend = NULL, *iohead = NULL;
  734. loff_t offset;
  735. unsigned long p_offset = 0;
  736. unsigned int type;
  737. __uint64_t end_offset;
  738. pgoff_t end_index, last_index, tlast;
  739. ssize_t size, len;
  740. int flags, err, iomap_valid = 0, uptodate = 1;
  741. int page_dirty, count = 0, trylock_flag = 0;
  742. int all_bh = unmapped;
  743. /* wait for other IO threads? */
  744. if (startio && (wbc->sync_mode == WB_SYNC_NONE && wbc->nonblocking))
  745. trylock_flag |= BMAPI_TRYLOCK;
  746. /* Is this page beyond the end of the file? */
  747. offset = i_size_read(inode);
  748. end_index = offset >> PAGE_CACHE_SHIFT;
  749. last_index = (offset - 1) >> PAGE_CACHE_SHIFT;
  750. if (page->index >= end_index) {
  751. if ((page->index >= end_index + 1) ||
  752. !(i_size_read(inode) & (PAGE_CACHE_SIZE - 1))) {
  753. if (startio)
  754. unlock_page(page);
  755. return 0;
  756. }
  757. }
  758. /*
  759. * page_dirty is initially a count of buffers on the page before
  760. * EOF and is decrememted as we move each into a cleanable state.
  761. *
  762. * Derivation:
  763. *
  764. * End offset is the highest offset that this page should represent.
  765. * If we are on the last page, (end_offset & (PAGE_CACHE_SIZE - 1))
  766. * will evaluate non-zero and be less than PAGE_CACHE_SIZE and
  767. * hence give us the correct page_dirty count. On any other page,
  768. * it will be zero and in that case we need page_dirty to be the
  769. * count of buffers on the page.
  770. */
  771. end_offset = min_t(unsigned long long,
  772. (xfs_off_t)(page->index + 1) << PAGE_CACHE_SHIFT, offset);
  773. len = 1 << inode->i_blkbits;
  774. p_offset = min_t(unsigned long, end_offset & (PAGE_CACHE_SIZE - 1),
  775. PAGE_CACHE_SIZE);
  776. p_offset = p_offset ? roundup(p_offset, len) : PAGE_CACHE_SIZE;
  777. page_dirty = p_offset / len;
  778. bh = head = page_buffers(page);
  779. offset = page_offset(page);
  780. flags = -1;
  781. type = 0;
  782. /* TODO: cleanup count and page_dirty */
  783. do {
  784. if (offset >= end_offset)
  785. break;
  786. if (!buffer_uptodate(bh))
  787. uptodate = 0;
  788. if (!(PageUptodate(page) || buffer_uptodate(bh)) && !startio) {
  789. /*
  790. * the iomap is actually still valid, but the ioend
  791. * isn't. shouldn't happen too often.
  792. */
  793. iomap_valid = 0;
  794. continue;
  795. }
  796. if (iomap_valid)
  797. iomap_valid = xfs_iomap_valid(&iomap, offset);
  798. /*
  799. * First case, map an unwritten extent and prepare for
  800. * extent state conversion transaction on completion.
  801. *
  802. * Second case, allocate space for a delalloc buffer.
  803. * We can return EAGAIN here in the release page case.
  804. *
  805. * Third case, an unmapped buffer was found, and we are
  806. * in a path where we need to write the whole page out.
  807. */
  808. if (buffer_unwritten(bh) || buffer_delay(bh) ||
  809. ((buffer_uptodate(bh) || PageUptodate(page)) &&
  810. !buffer_mapped(bh) && (unmapped || startio))) {
  811. /*
  812. * Make sure we don't use a read-only iomap
  813. */
  814. if (flags == BMAPI_READ)
  815. iomap_valid = 0;
  816. if (buffer_unwritten(bh)) {
  817. type = IOMAP_UNWRITTEN;
  818. flags = BMAPI_WRITE|BMAPI_IGNSTATE;
  819. } else if (buffer_delay(bh)) {
  820. type = IOMAP_DELAY;
  821. flags = BMAPI_ALLOCATE;
  822. if (!startio)
  823. flags |= trylock_flag;
  824. } else {
  825. type = IOMAP_NEW;
  826. flags = BMAPI_WRITE|BMAPI_MMAP;
  827. }
  828. if (!iomap_valid) {
  829. if (type == IOMAP_NEW) {
  830. size = xfs_probe_cluster(inode,
  831. page, bh, head, 0);
  832. } else {
  833. size = len;
  834. }
  835. err = xfs_map_blocks(inode, offset, size,
  836. &iomap, flags);
  837. if (err)
  838. goto error;
  839. iomap_valid = xfs_iomap_valid(&iomap, offset);
  840. }
  841. if (iomap_valid) {
  842. xfs_map_at_offset(bh, offset,
  843. inode->i_blkbits, &iomap);
  844. if (startio) {
  845. xfs_add_to_ioend(inode, bh, offset,
  846. type, &ioend,
  847. !iomap_valid);
  848. } else {
  849. set_buffer_dirty(bh);
  850. unlock_buffer(bh);
  851. mark_buffer_dirty(bh);
  852. }
  853. page_dirty--;
  854. count++;
  855. }
  856. } else if (buffer_uptodate(bh) && startio) {
  857. /*
  858. * we got here because the buffer is already mapped.
  859. * That means it must already have extents allocated
  860. * underneath it. Map the extent by reading it.
  861. */
  862. if (!iomap_valid || type != 0) {
  863. flags = BMAPI_READ;
  864. size = xfs_probe_cluster(inode, page, bh,
  865. head, 1);
  866. err = xfs_map_blocks(inode, offset, size,
  867. &iomap, flags);
  868. if (err)
  869. goto error;
  870. iomap_valid = xfs_iomap_valid(&iomap, offset);
  871. }
  872. type = 0;
  873. if (!test_and_set_bit(BH_Lock, &bh->b_state)) {
  874. ASSERT(buffer_mapped(bh));
  875. if (iomap_valid)
  876. all_bh = 1;
  877. xfs_add_to_ioend(inode, bh, offset, type,
  878. &ioend, !iomap_valid);
  879. page_dirty--;
  880. count++;
  881. } else {
  882. iomap_valid = 0;
  883. }
  884. } else if ((buffer_uptodate(bh) || PageUptodate(page)) &&
  885. (unmapped || startio)) {
  886. iomap_valid = 0;
  887. }
  888. if (!iohead)
  889. iohead = ioend;
  890. } while (offset += len, ((bh = bh->b_this_page) != head));
  891. if (uptodate && bh == head)
  892. SetPageUptodate(page);
  893. if (startio)
  894. xfs_start_page_writeback(page, wbc, 1, count);
  895. if (ioend && iomap_valid) {
  896. offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
  897. PAGE_CACHE_SHIFT;
  898. tlast = min_t(pgoff_t, offset, last_index);
  899. xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
  900. wbc, startio, all_bh, tlast);
  901. }
  902. if (iohead)
  903. xfs_submit_ioend(iohead);
  904. return page_dirty;
  905. error:
  906. if (iohead)
  907. xfs_cancel_ioend(iohead);
  908. /*
  909. * If it's delalloc and we have nowhere to put it,
  910. * throw it away, unless the lower layers told
  911. * us to try again.
  912. */
  913. if (err != -EAGAIN) {
  914. if (!unmapped)
  915. block_invalidatepage(page, 0);
  916. ClearPageUptodate(page);
  917. }
  918. return err;
  919. }
  920. STATIC int
  921. __linvfs_get_block(
  922. struct inode *inode,
  923. sector_t iblock,
  924. unsigned long blocks,
  925. struct buffer_head *bh_result,
  926. int create,
  927. int direct,
  928. bmapi_flags_t flags)
  929. {
  930. vnode_t *vp = LINVFS_GET_VP(inode);
  931. xfs_iomap_t iomap;
  932. xfs_off_t offset;
  933. ssize_t size;
  934. int retpbbm = 1;
  935. int error;
  936. offset = (xfs_off_t)iblock << inode->i_blkbits;
  937. if (blocks)
  938. size = (ssize_t) min_t(xfs_off_t, LONG_MAX,
  939. (xfs_off_t)blocks << inode->i_blkbits);
  940. else
  941. size = 1 << inode->i_blkbits;
  942. VOP_BMAP(vp, offset, size,
  943. create ? flags : BMAPI_READ, &iomap, &retpbbm, error);
  944. if (error)
  945. return -error;
  946. if (retpbbm == 0)
  947. return 0;
  948. if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
  949. xfs_daddr_t bn;
  950. xfs_off_t delta;
  951. /* For unwritten extents do not report a disk address on
  952. * the read case (treat as if we're reading into a hole).
  953. */
  954. if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  955. delta = offset - iomap.iomap_offset;
  956. delta >>= inode->i_blkbits;
  957. bn = iomap.iomap_bn >> (inode->i_blkbits - BBSHIFT);
  958. bn += delta;
  959. BUG_ON(!bn && !(iomap.iomap_flags & IOMAP_REALTIME));
  960. bh_result->b_blocknr = bn;
  961. set_buffer_mapped(bh_result);
  962. }
  963. if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
  964. if (direct)
  965. bh_result->b_private = inode;
  966. set_buffer_unwritten(bh_result);
  967. set_buffer_delay(bh_result);
  968. }
  969. }
  970. /* If this is a realtime file, data might be on a new device */
  971. bh_result->b_bdev = iomap.iomap_target->bt_bdev;
  972. /* If we previously allocated a block out beyond eof and
  973. * we are now coming back to use it then we will need to
  974. * flag it as new even if it has a disk address.
  975. */
  976. if (create &&
  977. ((!buffer_mapped(bh_result) && !buffer_uptodate(bh_result)) ||
  978. (offset >= i_size_read(inode)) || (iomap.iomap_flags & IOMAP_NEW)))
  979. set_buffer_new(bh_result);
  980. if (iomap.iomap_flags & IOMAP_DELAY) {
  981. BUG_ON(direct);
  982. if (create) {
  983. set_buffer_uptodate(bh_result);
  984. set_buffer_mapped(bh_result);
  985. set_buffer_delay(bh_result);
  986. }
  987. }
  988. if (blocks) {
  989. ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
  990. offset = min_t(xfs_off_t,
  991. iomap.iomap_bsize - iomap.iomap_delta,
  992. (xfs_off_t)blocks << inode->i_blkbits);
  993. bh_result->b_size = (u32) min_t(xfs_off_t, UINT_MAX, offset);
  994. }
  995. return 0;
  996. }
  997. int
  998. linvfs_get_block(
  999. struct inode *inode,
  1000. sector_t iblock,
  1001. struct buffer_head *bh_result,
  1002. int create)
  1003. {
  1004. return __linvfs_get_block(inode, iblock, 0, bh_result,
  1005. create, 0, BMAPI_WRITE);
  1006. }
  1007. STATIC int
  1008. linvfs_get_blocks_direct(
  1009. struct inode *inode,
  1010. sector_t iblock,
  1011. unsigned long max_blocks,
  1012. struct buffer_head *bh_result,
  1013. int create)
  1014. {
  1015. return __linvfs_get_block(inode, iblock, max_blocks, bh_result,
  1016. create, 1, BMAPI_WRITE|BMAPI_DIRECT);
  1017. }
  1018. STATIC void
  1019. linvfs_end_io_direct(
  1020. struct kiocb *iocb,
  1021. loff_t offset,
  1022. ssize_t size,
  1023. void *private)
  1024. {
  1025. xfs_ioend_t *ioend = iocb->private;
  1026. /*
  1027. * Non-NULL private data means we need to issue a transaction to
  1028. * convert a range from unwritten to written extents. This needs
  1029. * to happen from process contect but aio+dio I/O completion
  1030. * happens from irq context so we need to defer it to a workqueue.
  1031. * This is not nessecary for synchronous direct I/O, but we do
  1032. * it anyway to keep the code uniform and simpler.
  1033. *
  1034. * The core direct I/O code might be changed to always call the
  1035. * completion handler in the future, in which case all this can
  1036. * go away.
  1037. */
  1038. if (private && size > 0) {
  1039. ioend->io_offset = offset;
  1040. ioend->io_size = size;
  1041. xfs_finish_ioend(ioend);
  1042. } else {
  1043. ASSERT(size >= 0);
  1044. xfs_destroy_ioend(ioend);
  1045. }
  1046. /*
  1047. * blockdev_direct_IO can return an error even afer the I/O
  1048. * completion handler was called. Thus we need to protect
  1049. * against double-freeing.
  1050. */
  1051. iocb->private = NULL;
  1052. }
  1053. STATIC ssize_t
  1054. linvfs_direct_IO(
  1055. int rw,
  1056. struct kiocb *iocb,
  1057. const struct iovec *iov,
  1058. loff_t offset,
  1059. unsigned long nr_segs)
  1060. {
  1061. struct file *file = iocb->ki_filp;
  1062. struct inode *inode = file->f_mapping->host;
  1063. vnode_t *vp = LINVFS_GET_VP(inode);
  1064. xfs_iomap_t iomap;
  1065. int maps = 1;
  1066. int error;
  1067. ssize_t ret;
  1068. VOP_BMAP(vp, offset, 0, BMAPI_DEVICE, &iomap, &maps, error);
  1069. if (error)
  1070. return -error;
  1071. iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
  1072. ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
  1073. iomap.iomap_target->bt_bdev,
  1074. iov, offset, nr_segs,
  1075. linvfs_get_blocks_direct,
  1076. linvfs_end_io_direct);
  1077. if (unlikely(ret <= 0 && iocb->private))
  1078. xfs_destroy_ioend(iocb->private);
  1079. return ret;
  1080. }
  1081. STATIC sector_t
  1082. linvfs_bmap(
  1083. struct address_space *mapping,
  1084. sector_t block)
  1085. {
  1086. struct inode *inode = (struct inode *)mapping->host;
  1087. vnode_t *vp = LINVFS_GET_VP(inode);
  1088. int error;
  1089. vn_trace_entry(vp, "linvfs_bmap", (inst_t *)__return_address);
  1090. VOP_RWLOCK(vp, VRWLOCK_READ);
  1091. VOP_FLUSH_PAGES(vp, (xfs_off_t)0, -1, 0, FI_REMAPF, error);
  1092. VOP_RWUNLOCK(vp, VRWLOCK_READ);
  1093. return generic_block_bmap(mapping, block, linvfs_get_block);
  1094. }
  1095. STATIC int
  1096. linvfs_readpage(
  1097. struct file *unused,
  1098. struct page *page)
  1099. {
  1100. return mpage_readpage(page, linvfs_get_block);
  1101. }
  1102. STATIC int
  1103. linvfs_readpages(
  1104. struct file *unused,
  1105. struct address_space *mapping,
  1106. struct list_head *pages,
  1107. unsigned nr_pages)
  1108. {
  1109. return mpage_readpages(mapping, pages, nr_pages, linvfs_get_block);
  1110. }
  1111. STATIC void
  1112. xfs_count_page_state(
  1113. struct page *page,
  1114. int *delalloc,
  1115. int *unmapped,
  1116. int *unwritten)
  1117. {
  1118. struct buffer_head *bh, *head;
  1119. *delalloc = *unmapped = *unwritten = 0;
  1120. bh = head = page_buffers(page);
  1121. do {
  1122. if (buffer_uptodate(bh) && !buffer_mapped(bh))
  1123. (*unmapped) = 1;
  1124. else if (buffer_unwritten(bh) && !buffer_delay(bh))
  1125. clear_buffer_unwritten(bh);
  1126. else if (buffer_unwritten(bh))
  1127. (*unwritten) = 1;
  1128. else if (buffer_delay(bh))
  1129. (*delalloc) = 1;
  1130. } while ((bh = bh->b_this_page) != head);
  1131. }
  1132. /*
  1133. * writepage: Called from one of two places:
  1134. *
  1135. * 1. we are flushing a delalloc buffer head.
  1136. *
  1137. * 2. we are writing out a dirty page. Typically the page dirty
  1138. * state is cleared before we get here. In this case is it
  1139. * conceivable we have no buffer heads.
  1140. *
  1141. * For delalloc space on the page we need to allocate space and
  1142. * flush it. For unmapped buffer heads on the page we should
  1143. * allocate space if the page is uptodate. For any other dirty
  1144. * buffer heads on the page we should flush them.
  1145. *
  1146. * If we detect that a transaction would be required to flush
  1147. * the page, we have to check the process flags first, if we
  1148. * are already in a transaction or disk I/O during allocations
  1149. * is off, we need to fail the writepage and redirty the page.
  1150. */
  1151. STATIC int
  1152. linvfs_writepage(
  1153. struct page *page,
  1154. struct writeback_control *wbc)
  1155. {
  1156. int error;
  1157. int need_trans;
  1158. int delalloc, unmapped, unwritten;
  1159. struct inode *inode = page->mapping->host;
  1160. xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
  1161. /*
  1162. * We need a transaction if:
  1163. * 1. There are delalloc buffers on the page
  1164. * 2. The page is uptodate and we have unmapped buffers
  1165. * 3. The page is uptodate and we have no buffers
  1166. * 4. There are unwritten buffers on the page
  1167. */
  1168. if (!page_has_buffers(page)) {
  1169. unmapped = 1;
  1170. need_trans = 1;
  1171. } else {
  1172. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1173. if (!PageUptodate(page))
  1174. unmapped = 0;
  1175. need_trans = delalloc + unmapped + unwritten;
  1176. }
  1177. /*
  1178. * If we need a transaction and the process flags say
  1179. * we are already in a transaction, or no IO is allowed
  1180. * then mark the page dirty again and leave the page
  1181. * as is.
  1182. */
  1183. if (PFLAGS_TEST_FSTRANS() && need_trans)
  1184. goto out_fail;
  1185. /*
  1186. * Delay hooking up buffer heads until we have
  1187. * made our go/no-go decision.
  1188. */
  1189. if (!page_has_buffers(page))
  1190. create_empty_buffers(page, 1 << inode->i_blkbits, 0);
  1191. /*
  1192. * Convert delayed allocate, unwritten or unmapped space
  1193. * to real space and flush out to disk.
  1194. */
  1195. error = xfs_page_state_convert(inode, page, wbc, 1, unmapped);
  1196. if (error == -EAGAIN)
  1197. goto out_fail;
  1198. if (unlikely(error < 0))
  1199. goto out_unlock;
  1200. return 0;
  1201. out_fail:
  1202. redirty_page_for_writepage(wbc, page);
  1203. unlock_page(page);
  1204. return 0;
  1205. out_unlock:
  1206. unlock_page(page);
  1207. return error;
  1208. }
  1209. STATIC int
  1210. linvfs_invalidate_page(
  1211. struct page *page,
  1212. unsigned long offset)
  1213. {
  1214. xfs_page_trace(XFS_INVALIDPAGE_ENTER,
  1215. page->mapping->host, page, offset);
  1216. return block_invalidatepage(page, offset);
  1217. }
  1218. /*
  1219. * Called to move a page into cleanable state - and from there
  1220. * to be released. Possibly the page is already clean. We always
  1221. * have buffer heads in this call.
  1222. *
  1223. * Returns 0 if the page is ok to release, 1 otherwise.
  1224. *
  1225. * Possible scenarios are:
  1226. *
  1227. * 1. We are being called to release a page which has been written
  1228. * to via regular I/O. buffer heads will be dirty and possibly
  1229. * delalloc. If no delalloc buffer heads in this case then we
  1230. * can just return zero.
  1231. *
  1232. * 2. We are called to release a page which has been written via
  1233. * mmap, all we need to do is ensure there is no delalloc
  1234. * state in the buffer heads, if not we can let the caller
  1235. * free them and we should come back later via writepage.
  1236. */
  1237. STATIC int
  1238. linvfs_release_page(
  1239. struct page *page,
  1240. gfp_t gfp_mask)
  1241. {
  1242. struct inode *inode = page->mapping->host;
  1243. int dirty, delalloc, unmapped, unwritten;
  1244. struct writeback_control wbc = {
  1245. .sync_mode = WB_SYNC_ALL,
  1246. .nr_to_write = 1,
  1247. };
  1248. xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, gfp_mask);
  1249. xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
  1250. if (!delalloc && !unwritten)
  1251. goto free_buffers;
  1252. if (!(gfp_mask & __GFP_FS))
  1253. return 0;
  1254. /* If we are already inside a transaction or the thread cannot
  1255. * do I/O, we cannot release this page.
  1256. */
  1257. if (PFLAGS_TEST_FSTRANS())
  1258. return 0;
  1259. /*
  1260. * Convert delalloc space to real space, do not flush the
  1261. * data out to disk, that will be done by the caller.
  1262. * Never need to allocate space here - we will always
  1263. * come back to writepage in that case.
  1264. */
  1265. dirty = xfs_page_state_convert(inode, page, &wbc, 0, 0);
  1266. if (dirty == 0 && !unwritten)
  1267. goto free_buffers;
  1268. return 0;
  1269. free_buffers:
  1270. return try_to_free_buffers(page);
  1271. }
  1272. STATIC int
  1273. linvfs_prepare_write(
  1274. struct file *file,
  1275. struct page *page,
  1276. unsigned int from,
  1277. unsigned int to)
  1278. {
  1279. return block_prepare_write(page, from, to, linvfs_get_block);
  1280. }
  1281. struct address_space_operations linvfs_aops = {
  1282. .readpage = linvfs_readpage,
  1283. .readpages = linvfs_readpages,
  1284. .writepage = linvfs_writepage,
  1285. .sync_page = block_sync_page,
  1286. .releasepage = linvfs_release_page,
  1287. .invalidatepage = linvfs_invalidate_page,
  1288. .prepare_write = linvfs_prepare_write,
  1289. .commit_write = generic_commit_write,
  1290. .bmap = linvfs_bmap,
  1291. .direct_IO = linvfs_direct_IO,
  1292. .migratepage = buffer_migrate_page,
  1293. };